package com.cheyun.netsalev3.utils;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;

/**
 * @author Michael
 * @date 2018/12/11 - 11:50.
 */
public class IndicatorLineUtil {
    /**
     * 调节tablayout指示线宽度
     *
     * @param tabs
     */
    public static void setIndicator(TabLayout tabs, int width, int leftrightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("slidingTabIndicator");
        } catch (NoSuchFieldException e) {
            Log.e("IndicatorLineUtil",e.getMessage());
            return;
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            Log.e("IndicatorLineUtil",e.getMessage());
            return;
        }

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params;
            if (width == 0) {
                params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            } else {
                params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
            }
            params.leftMargin = leftrightDip;
            params.rightMargin = leftrightDip;
            child.setLayoutParams(params);
            child.invalidate();
        }


    }

}
